/******************************************************************************
 * Copyright (C) 2017 Chengdu Dlhd information industry co. LTD.
 * All Rights Reserved.
 * 本软件为成都顶联互动信息产业有限公司开发研制。未经本公司正式书面同意，其他任何个人、团体不得使用、复制、修改或发布本软件.
 *****************************************************************************/
package net.dgg.dggcloud.service.iboss.service.sc;

import net.dgg.dggcloud.core.minidao.hibernate.dao.IGenericBaseCommonDao;
import net.dgg.dggcloud.service.common.base.service.BaseService;
import net.dgg.dggcloud.service.entity.user.User;
import net.dgg.dggcloud.service.iboss.dao.sc.NodeManagerDao;
import net.dgg.dggcloud.service.iboss.entity.ScProFlowNode;
import net.dgg.dggcloud.service.iboss.entity.ScProNodeFlow;
import net.dgg.dggcloud.service.jrcrm.service.cms.BillNumberRuleService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @class_name: NodeManagerService
 * @package: net.dgg.dggcloud.service.iboss.service.sc
 * @describe: TODO 节点管理——service层
 * @creat_user: 代杨
 * @creat_date: 2017/11/27
 * @creat_time: 15:20
 **/
@Service
public class NodeManagerService extends BaseService {
    @Resource(name = "miniDaoHiberCommonDao")
    protected IGenericBaseCommonDao repository;
    @Resource
    private NodeManagerDao nodeManagerDao;
    @Autowired
    private BillNumberRuleService billNumberRuleService;
    /**
     * @Title:
     * @Description: TODO 流程列表
     * @param:
     * @return:
     * @throw:
     * @creat_user: 代杨
     * @creat_date: 2017/11/27
     **/
    public List<Map> listNodemanagerByMap(Map map) {
        return nodeManagerDao.listNodeManagerByMap(map);
    }

    /**
     * @Title:
     * @Description: TODO 统计流程数量
     * @param:
     * @return:
     * @throw:
     * @creat_user: 代杨
     * @creat_date: 2017/11/28
     **/
    public Integer countNodemanagerByMap(Map map) {
        return nodeManagerDao.countNodeManagerByMap(map);
    }

    /**
     * @Title:
     * @Description: TODO 查询出所有节点字段
     * @param:
     * @return:
     * @throw:
     * @creat_user: 代杨
     * @creat_date: 2017/11/28
     **/
    public List<Map> queryFieldList() {
        List<Map> retMap = nodeManagerDao.queryFieldList();
        return retMap;
    }

    /**
     * @Title:
     * @Description: TODO 根据事业部业态查询出流程数量
     * @param:
     * @return:
     * @throw:
     * @creat_user: 代杨
     * @creat_date: 2017/12/8
     **/
    public Integer CheckProcessIsExistence(Map map) {
        Integer retnum = nodeManagerDao.countProcessByMap(map);
        return retnum;
    }

    /**
     * @Title:
     * @Description: TODO 增加流程保存
     * @param:
     * @return:
     * @throw:
     * @creat_user: 代杨
     * @creat_date: 2017/12/5
     **/
    public void addprocess(Map map, User user) {
        long id = this.keyWorker.nextId();
        ScProNodeFlow scProNodeFlow = new ScProNodeFlow();
        scProNodeFlow.setId(id);
        scProNodeFlow.setFlowNo(this.billNumberRuleService.updateBillNumberByCode("LC"));
        scProNodeFlow.setDeptIds(Long.parseLong(map.get("busdept").toString()));
        scProNodeFlow.setDeptNames(map.get("divisiontext").toString());
        scProNodeFlow.setBusinessCode(map.get("busyt").toString());
        scProNodeFlow.setBusinessName(map.get("busyttext").toString());
        int bldate=Integer.parseInt(map.get("bldate").toString());
        Assert.isTrue(bldate >0, "办理周期必须大于0！");
        scProNodeFlow.setManagementCycle(bldate);
        scProNodeFlow.setFlag(1);
        scProNodeFlow.setCreateUser(user);
        scProNodeFlow.setUpdaterUser(user);

        Integer retnum = CheckProcessIsExistence(map);
        Assert.isTrue(retnum < 1, "该事业部所属业态的流程已存在，无法创建！");
        repository.save(scProNodeFlow);
    }
    /**
     * @Title:
     * @Description: TODO 编辑流程新增保存
     * @param:
     * @return:
     * @throw:
     * @creat_user: 代杨
     * @creat_date: 2017/12/5
     **/
    public void updateeditprocess(Map map, User user) {
        Map tjmap = new HashMap();
        Long processid = Long.parseLong(map.get("processid").toString());
        ScProNodeFlow scProNodeFlow = queryScProNodeFlow(processid);
        scProNodeFlow.setDeptIds(Long.parseLong(map.get("busdept").toString()));
        scProNodeFlow.setDeptNames(map.get("divisiontext").toString());
        scProNodeFlow.setBusinessCode(map.get("busyt").toString());
        scProNodeFlow.setBusinessName(map.get("busyttext").toString());

        int bldate=Integer.parseInt(map.get("bldate").toString());
        Assert.isTrue(bldate >0, "办理周期必须大于0！");
        scProNodeFlow.setManagementCycle(bldate);
        scProNodeFlow.setUpdaterUser(user);

        JSONArray jsonArray = JSONArray.fromObject(map.get("jsonData"));
        if (jsonArray.size() > 0) {
            StringBuffer str = new StringBuffer();
            for (int i = 0; i < jsonArray.size(); i++) {
                ScProFlowNode scProFlowNode = new ScProFlowNode();
                JSONObject job = jsonArray.getJSONObject(i);  // 遍历 jsonarray 数组，把每一个对象转成 json 对象
                Long nodeid = job.get("nodeid") == null ? this.keyWorker.nextId() : Long.parseLong(job.get("nodeid").toString());
                scProFlowNode.setId(nodeid);
                scProFlowNode.setName(job.get("Nodename").toString());
                scProFlowNode.setSort(Integer.parseInt(job.get("Xuhao").toString()));
                scProFlowNode.setProcessId(processid);

                int handledate=Integer.parseInt(job.get("Nodecycle").toString());
                Assert.isTrue(handledate >0, "节点办理周期必须大于0！");
                scProFlowNode.setHandledate(handledate);
                scProFlowNode.setRelationfield(job.get("Nodecruxfield").toString());
                scProFlowNode.setRelationfieldids(job.get("nodecruxfieldids").toString());
                scProFlowNode.setIsKey(Integer.parseInt(job.get("Iscurxnode").toString()));
                scProFlowNode.setFlag(1);
                if (job.get("nodeid") == null) {
                    scProFlowNode.setCreateUser(user);
                } else {
                    scProFlowNode.setUpdaterUser(user);
                }
                str.append(nodeid);
                if (i != jsonArray.size() - 1) {
                    str.append(",");
                }
                repository.saveOrUpdate(scProFlowNode);
            }
            tjmap.put("nodeids", str.toString());
            tjmap.put("processid", processid);
            nodeManagerDao.updateNodeByIds(tjmap);
        }
        Integer retnum = CheckProcessIsExistence(map);
        Assert.isTrue(retnum <= 1, "该事业部所属业态的流程已存在，无法创建！");
        repository.saveOrUpdate(scProNodeFlow);
    }
    /**
     * @Title:
     * @Description: TODO 复制流程信息保存
     * @param:
     * @return:
     * @throw:
     * @creat_user: 代杨
     * @creat_date: 2017/12/20
     **/
    public void addcopyprocess(Map map, User user) {
        Long processid = Long.parseLong(map.get("processid").toString());
        long id = this.keyWorker.nextId();
        ScProNodeFlow scProNodeFlow = new ScProNodeFlow();
        scProNodeFlow.setId(id);
        scProNodeFlow.setFlowNo(this.billNumberRuleService.updateBillNumberByCode("LC"));
        scProNodeFlow.setDeptIds(Long.parseLong(map.get("busdept").toString()));
        scProNodeFlow.setDeptNames(map.get("divisiontext").toString());
        scProNodeFlow.setBusinessCode(map.get("busyt").toString());
        scProNodeFlow.setBusinessName(map.get("busyttext").toString());
        scProNodeFlow.setManagementCycle(Integer.parseInt(map.get("bldate").toString()));
        scProNodeFlow.setFlag(1);
        scProNodeFlow.setCreateUser(user);
        scProNodeFlow.setUpdaterUser(user);

        Map tjmap = new HashMap();
        tjmap.put("processid", processid+"");
        List<Map> retMap =queryNodeList(tjmap);
        if(retMap.size()>0){
            for (int i = 0; i <retMap.size() ; i++) {
                ScProFlowNode scProFlowNode = new ScProFlowNode();
                Map nodemap=retMap.get(i);
                Assert.notNull(nodemap.get("id"),"节点ID关联错误，请联系管理员！");
                Long nodeid = this.keyWorker.nextId();
                scProFlowNode.setId(nodeid);
                scProFlowNode.setName(nodemap.get("name").toString());
                scProFlowNode.setSort(Integer.parseInt(nodemap.get("sort").toString()));
                scProFlowNode.setProcessId(id);
                scProFlowNode.setHandledate(Integer.parseInt(nodemap.get("handledate").toString()));
                scProFlowNode.setRelationfield(nodemap.get("relationfield").toString());
                scProFlowNode.setRelationfieldids(nodemap.get("relationfieldids").toString());
                scProFlowNode.setIsKey(Integer.parseInt(nodemap.get("iskey").toString()));
                scProFlowNode.setFlag(1);
                scProFlowNode.setCreateUser(user);
                repository.saveOrUpdate(scProFlowNode);
            }
        }
        Integer retnum = CheckProcessIsExistence(map);
        Assert.isTrue(retnum < 1, "该事业部所属业态的流程已存在，无法创建！");
        repository.saveOrUpdate(scProNodeFlow);
    }

    /**
     * @Title:
     * @Description: TODO 根据id查询出流程
     * @param:
     * @return:
     * @throw:
     * @creat_user: 代杨
     * @creat_date: 2017/12/5
     **/
    public ScProNodeFlow queryScProNodeFlow(Long id) {
        ScProNodeFlow scProNodeFlow = repository.get(ScProNodeFlow.class, id);
        return scProNodeFlow;
    }

    /**
     * @Title:
     * @Description: TODO 根据id查询出节点信息
     * @param:
     * @return:
     * @throw:
     * @creat_user: 代杨
     * @creat_date: 2017/12/5
     **/
    public ScProFlowNode queryScProNodeById(Long id) {
        ScProFlowNode scProFlowNode = repository.get(ScProFlowNode.class, id);
        return scProFlowNode;
    }

    /**
     * @Title:
     * @Description: TODO 根据流程ID查询出最大节点
     * @param:
     * @return:
     * @throw:
     * @creat_user: 代杨
     * @creat_date: 2017/12/20
     **/
    public  List<Map> queryMaxProNodeById(Map map) {
        List<Map> retMap = nodeManagerDao.queryMaxProNodeById(map);
        return retMap;
    }
    /**
     * @Title:
     * @Description: TODO 查询出是否包含该节点的操作记录
     * @param:
     * @return:
     * @throw:
     * @creat_user: 代杨
     * @creat_date: 2017/12/12
     **/
    public List<Map> queryScOperationByMap(Map map) {
        List<Map> retMap = nodeManagerDao.queryScOperationByMap(map);
        return retMap;
    }

    /**
     * @Title:
     * @Description: TODO 根据生产订单id、流程id和序号查询出节点信息
     * @param:
     * @return:
     * @throw:
     * @creat_user: 代杨
     * @creat_date: 2017/12/8
     **/
    public List<Map> queryNextNodeList(Map map) {
        List<Map> retMap = nodeManagerDao.queryNextNodeList(map);
        return retMap;
    }

    /**
     * @Title:
     * @Description: TODO 根据流程id和序号查询出剩余关键节点
     * @param:
     * @return:
     * @throw:
     * @creat_user: 代杨
     * @creat_date: 2017/12/8
     **/
    public List<Map> queryIskeyNodeList(Map map) {
        List<Map> retMap = nodeManagerDao.queryIskeyNodeList(map);
        return retMap;
    }
    /**
     * @Title:
     * @Description: TODO 根据流程id查询出对应的节点列表
     * @param:
     * @return:
     * @throw:
     * @creat_user: 代杨
     * @creat_date: 2017/12/5
     **/
    public List<Map> queryNodeList(Map map) {
        List<Map> retMap = nodeManagerDao.queryNodeList(map);
        return retMap;
    }

    /**
     * @Title:
     * @Description: TODO 根据事业部查询出下面所有的流程节点
     * @param:
     * @return:
     * @throw:
     * @creat_user: 代杨
     * @creat_date: 2017/12/5
     **/
    public List<Map> queryAllNodeListByOrg(Map map) {
        List<Map> retMap = nodeManagerDao.queryAllNodeListByOrg(map);
        return retMap;
    }
    /**
     * @Title:
     * @Description: TODO 根据事业部查询出对应业态
     * @param:
     * @return:
     * @throw:
     * @creat_user: 代杨
     * @creat_date: 2017/12/15
     **/
    public List<Map> queryYtinforByOrg(Map map) {
        List<Map> retMap = nodeManagerDao.queryYtinforByOrg(map);
        return retMap;
    }
    /**
     * @Title:
     * @Description: TODO 根据事业部查询出对应节点
     * @param:
     * @return:
     * @throw:
     * @creat_user: 代杨
     * @creat_date: 2017/12/15
     **/
    public List<Map> queryAllNodeByDivision(Map map) {
        List<Map> retMap = nodeManagerDao.queryAllNodeByDivision(map);
        return retMap;
    }

    /**
     * @Title:
     * @Description: TODO 根据生产id和节点id查询出操作信息
     * @param:
     * @return:
     * @throw:
     * @creat_user: 代杨
     * @creat_date: 2017/12/7
     **/
    public List<Map> queryOperationinfo(Map map) {
        List<Map> retMap = nodeManagerDao.queryOperationinfoByProidAndNodeid(map);
        return retMap;
    }
}
